package com.tanger.im.service.group;

import com.tanger.common.exception.AbstractController;
import com.tanger.common.result.ActionResult;
import com.tanger.common.utils.StringUtil;
import com.tanger.im.entity.ImUserGroup;
import com.tanger.im.entity.IMUserGroupDto;
import com.tanger.im.entity.IMUserGroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Description 用户控制层
 * @Author Hchenbin
 * @Date 2023/12/26 9:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/tanger/api/im/user:group")
@Api(value = "IM:IM用户群/组",tags="IM:IM用户群/组")
public class IMUserGroupController extends AbstractController{

    @Autowired
    private IMUserGroupService service;

    @ApiImplicitParam(name = "userId", value = "IM用户ID", required = true, paramType = "query" ,dataType = "Long")
    @ApiOperation(value = "所在组群列表")
    @GetMapping("/findAll")
    public ActionResult<List<ImUserGroup>> findAll(@RequestParam Long userId){
        if (StringUtil.isNull(userId)) {
            return ActionResult.buildResultError("参数缺失");
        }
        return ActionResult.buildResultSuccess(service.findAll(userId));
    }

    @ApiImplicitParam(name = "id", value = "组群ID", required = true, paramType = "query" ,dataType = "Long")
    @ApiOperation(value = "组群详情")
    @GetMapping("/findOne")
    public ActionResult<IMUserGroupDto> findOne(@RequestParam Long id){
        if (StringUtil.isNull(id)) {
            return ActionResult.buildResultError("参数缺失");
        }
        return ActionResult.buildResultSuccess(service.findOne(id));
    }

    @ApiOperation(value = "新建组群")
    @PostMapping("/save")
    public ActionResult save(@RequestBody IMUserGroupVO vo) {
        if (Objects.isNull(vo) || CollectionUtils.isEmpty(vo.getMembersArr())
                || StringUtil.isNull(vo.getName()) || StringUtil.isNull(vo.getIcon())) {
            return ActionResult.buildResultError("参数缺失");
        }
        return service.save(vo);
    }

    @ApiOperation(value = "编辑组群")
    @PatchMapping("/update")
    public ActionResult update(@RequestBody IMUserGroupVO vo) {
        if (Objects.isNull(vo) || StringUtil.isNull(vo.getId())) {
            return ActionResult.buildResultError("参数缺失");
        }
        return service.update(vo);
    }

    @ApiImplicitParam(name = "id", value = "组群ID", required = true, paramType = "query" ,dataType = "Long")
    @ApiOperation(value = "解散组群")
    @DeleteMapping("/cancel")
    public ActionResult cancel(@RequestParam Long id) {
        if (StringUtil.isNull(id)) {
            return ActionResult.buildResultError("参数缺失");
        }
        return service.cancel(id);
    }
}

